/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.server.data;

import com.google.appengine.api.datastore.Key;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gwtportlets.portlet.smartgwt.client.CriteriaDto;
import org.gwtportlets.portlet.smartgwt.client.DataTransferObject;
import org.gwtportlets.portlet.smartgwt.client.SmartPortletFactory;
import org.gwtportlets.portlet.smartgwt.server.DataProviderObject;
import org.gwtportlets.portlet.smartgwt.server.SmartFilter;
import za.org.foobar.famon.frontend.client.FamonException;
import za.org.foobar.famon.frontend.client.dto.ApplicationUserDto;
import za.org.foobar.famon.frontend.server.ApplicationLogic;
import za.org.foobar.famon.model.Application;
import za.org.foobar.famon.model.ApplicationUser;
import za.org.foobar.famon.model.UserData;
import za.org.foobar.famon.util.DB;

import java.util.List;

public class ApplicationUserDpo implements DataProviderObject {
    private final Log log = LogFactory.getLog(getClass());

    public String getDataSourceId() {
        return ApplicationUserDto.DATA_SOURCE_ID;
    }

    public List<DataTransferObject> fetchData(SmartPortletFactory f) {
        CriteriaDto criteria = f.getCriteria();
        if (criteria == null) {
            throw new FamonException("No criteria provided in ApplicationUserFetch");
        }
        log.info("Fetch: Criteria:\n" + criteria);
        String applicationId = SmartFilter.getSimpleCriteriaValue(criteria, "applicationId");
        if (applicationId != null) {
            ApplicationUserDto dto = new ApplicationUserDto();
            dto.applicationId = applicationId;
            return f.singleResultList(dto);
        }

        String idString = SmartFilter.getSimpleCriteriaValue(criteria, "id");

        if (idString == null) {
            throw new FamonException("Id not found");
        }
        ApplicationUser applicationUser = DB.findEx(ApplicationUser.class, ApplicationUser.stringToKey(null, idString));
        ApplicationUserDto dto = new ApplicationUserDto();
        dto.id = applicationUser.keyToString();
        dto.applicationId = applicationUser.getApplication().keyToString();
        dto.userId = applicationUser.getUser().keyToString();
        dto.role = applicationUser.getRole();
        return f.singleResultList(dto);
    }

    public DataTransferObject updateData(SmartPortletFactory f) {
        ApplicationUserDto dto = (ApplicationUserDto)f.getSingleDto();
        ApplicationUser applicationUser = DB.findEx(ApplicationUser.class, ApplicationUser.stringToKey(null, dto.id));
        applicationUser.setRole(dto.role);
        DB.persist(applicationUser);                                                            
        return dto;
    }

    public DataTransferObject addData(SmartPortletFactory f) {
        CriteriaDto criteria = f.getCriteria();
        log.info("Add: Criteria:\n" + criteria);
        ApplicationUserDto dto = (ApplicationUserDto)f.getSingleDto();
        ApplicationLogic logic = ApplicationLogic.get();
        Application app = logic.getApplication(Application.stringToKey(dto.applicationId));
        Key userKey = UserData.stringToKey(dto.userId);
        ApplicationUser applicationUser = logic.addApplicationUser(app, userKey);
        return logic.createApplicationUserDto(applicationUser);
    }

    public DataTransferObject removeData(SmartPortletFactory f) {
        CriteriaDto criteria = f.getCriteria();
        log.info("Remove: Criteria:\n" + criteria);
        ApplicationUserDto dto = (ApplicationUserDto)f.getSingleDto();
        DB.findEx(ApplicationUser.class, ApplicationUser.stringToKey(null, dto.id)).delete();
        return dto;
    }
}
